<?php
class Commanswer extends Base_Controller
{
    public function __construct()
    {
        /********待编辑********/
        $this->_page_size = 5;
        $this->_form_type = 'comma';
        $this->_list_where = array();
        $this->_action_list = array('EDIT');
        $this->_controller = 'admin/commanswer/';
        parent::__construct();
    }

    public function showList($qid = 0,$page = 1)
    {
        $this->_external_id = $qid;
        $this->_list_where = array('a.qid'=>$qid);

        $get = $this->input->get();
        if (!empty($get)){
            $where = array();
            $start = !empty($get['start'])?strtotime(trim($get['start'])):'';
            $end = !empty($get['end'])?strtotime(trim($get['end'])):'';
            $user = !empty($get['fusername'])?trim($get['fusername']):'';
            if (!empty($start)) $where['a.changed >'] = $start;
            if (!empty($end)) $where['a.changed <'] = $end;
            if (!empty($user)) $where['u.userName LIKE']= '%'.$user.'%';
            $this->_list_where = array_merge($this->_list_where,$where);
            $filter = $this->load->view('admin/form/filter/alistfilter',array('start'=>$get['start'],'end'=>$get['end'],'fusername'=>$user),true);
        }else{
            $filter = $this->load->view('admin/form/filter/alistfilter',array(),true);
        }

        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/'.$this->_external_id.'/');

        $res = $this->getList($offset,$limit);

        $createbtn = $this->load->view('admin/form/createbtn',array('controller'=>site_url($this->_controller),'param'=>$this->_external_id),true);
        $backbtn = $this->load->view('admin/form/backbtn',array('controller'=>site_url('admin/commquestion/')),true);
        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'filter'=>$filter,'createbtn'=>$createbtn,'backbtn'=>$backbtn,'pagination'=>$pdata['pagination']);

        $content = $this->load->view('admin/form/list',$data,true);
        echo $this->getAdminView($content);

    }
    //重写获取list数据
    public function getList($offset = 0,$limit = 0)
    {
        $this->db->select('a.id,u.userName,a.answer,a.changed')->from('comm_answer as a')->join('mod_user as u','u.uid = a.uid','left');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        if (!empty($limit)) $this->db->limit($limit,$offset);
        $this->db->order_by('changed','DESC');
        $data=$this->db->get()->result_array();

        $res = array('headers'=>array(),'list'=>array());
        foreach($data as $row){
            $rowdata = array();
            $rowdata['userName']=$row['userName'];
            $rowdata['answer']='<div class="txttoggle-wrap flex-column flex-align-center flex-justify-around flex-nowrap">'.
                '<div class="txttoggle-content paragraph-cut">'.$row['answer'].'</div>'.
                '<div class="txttoggle-icon"></div></div>';
            $rowdata['changed']=date('Y-m-d H:i:s',$row['changed']);
            $rowdata['actionlist']=$this->adminform_model->getActionList($this->_action_list,$this->_controller,$row['id'],$this->_external_id);
            $res['list'][]=$rowdata;
        }
        $res['headers']=array('回答者','回答内容','创建时间','操作');
        return $res;
    }

    public function getPagination($uri = '')
    {
        $this->db->from('comm_answer as a')->join('mod_user as u','u.uid = a.uid','left');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        $total = $this->db->count_all_results();

        $this->load->library('pagination');
        $config['base_url'] = site_url($uri);
        $config['total_rows'] = $total;
        $config['per_page'] = $this->_page_size;
        $config['suffix'] = '?'.$_SERVER['QUERY_STRING'];
        $config['first_url'] = $config['base_url'].'?'.$_SERVER['QUERY_STRING'];
        $config['cur_tag_open'] = '<div class="cur-page">';
        $config['cur_tag_close'] = '</div>';
        $config['use_page_numbers'] = TRUE;
        $config['num_tag_open'] = '<div class="pageitem">';
        $config['num_tag_close'] = '</div>';
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<div class="pre-page">';
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<div class="next-page">';
        $config['next_tag_close'] = '</div>';
        $config['last_link'] = '最后一页';
        $config['last_tag_open'] = '<div class="last-page">';
        $config['last_tag_close'] = '</div>';
        $config['first_link'] = '第一页';
        $config['first_tag_open'] = '<div class="first-page">';
        $config['first_tag_close'] = '</div>';
        $config['full_tag_open'] = '<div class="page-total-results">共'.$total.'条，每页'.$this->_page_size.'条</div>';

        $this->pagination->initialize($config);

        return array('total'=>$total,'pagination'=>$this->pagination->create_links());
    }

    public function update($id = 0,$qid = 0)
    {
        $this->_external_id = $qid;
        $post = $this->input->post();

        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),
                'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /********待编辑********/
            //filter post data
            $post['changed'] = time();

            $this->_update_data = $post;
            parent::update($id);
        }
    }

    public function create($qid = 0)
    {
        $this->_external_id = $qid;
        $post = $this->input->post();

        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),
                'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /********待编辑********/
            //filter post data
            $uid = $this->_uid;
            $user = $this->adminuser_model->getUser($uid);
            $post['uid'] = (int)$user['commid'];
            $this->_commid = $post['uid'];
            $post['qid'] = $qid;
            $post['created'] = time();
            $post['changed'] = time();

            $this->_create_data = $post;
            parent::create();
        }
    }
    public function aftercreate($id)
    {
        $res = $this->adminform_model->getRowData('comm_answer','uid,qid',array('id'=>$id));
        $this->load->model('redis_model');
        $redis = $this->redis_model->getRedisInstance();
        $commid = $this->_commid;
        $redis->multi()
            ->set('answer:'.$id.'.agreesraw','')
            ->hMSet('answer:'.$id,array('id'=>$id,'uid'=>$res['uid'],'qid'=>$res['qid'],'agrees'=>0,'comments'=>0,'collects'=>0,'created'=>time()))
            ->sAdd('user:'.$commid.'.answers',$this->_external_id)
            ->hIncrBy('question:'.$this->_external_id,'answers',1)
            ->exec();
    }

    public function delete($id = 0,$qid = 0){return false;}
}